{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests                                                        #调用 requests\n",
    "api_secret = \"72ZSxF3jN4mKki4f6kcTCDIjlEvcwi4S\"                        #填入从face++获得的api_secret\n",
    "api_key = 'n5iuIaeoc-HEqvbWSEMxc73zfy092FdR'                           #填入从face++上获得的api密钥\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect'          #face++人脸识别的入口\n",
    "img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603529269698&di=a923592501cfbe6305e7cf76a85b590b&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F31%2F20181031164830_u4NY2.jpeg'\n",
    "                                                                       #填入想要检测的图片\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion',                  #要检测的人脸的特征\n",
    "}\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)           #发出请求\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603519967,61b9cb27-7a87-4322-9f26-c2c425afda40\",\"time_used\":3194,\"faces\":[{\"face_token\":\"c1e2fa800f1f2ebc3353294931dc3582\",\"face_rectangle\":{\"top\":355,\"left\":1102,\"width\":236,\"height\":236},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":34},\"smile\":{\"value\":94.764,\"threshold\":50.000},\"emotion\":{\"anger\":0.023,\"disgust\":0.006,\"fear\":0.007,\"happiness\":99.334,\"neutral\":0.582,\"sadness\":0.006,\"surprise\":0.042}}},{\"face_token\":\"f0148088a9e1859724499492aae6963d\",\"face_rectangle\":{\"top\":222,\"left\":274,\"width\":225,\"height\":225},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":20},\"smile\":{\"value\":7.332,\"threshold\":50.000},\"emotion\":{\"anger\":0.005,\"disgust\":0.000,\"fear\":0.000,\"happiness\":0.037,\"neutral\":99.958,\"sadness\":0.000,\"surprise\":0.000}}}],\"image_id\":\"ZTtQORZzaHTUrpqCHAlhMw==\",\"face_num\":2}\\n'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603519967,61b9cb27-7a87-4322-9f26-c2c425afda40',\n",
       " 'time_used': 3194,\n",
       " 'faces': [{'face_token': 'c1e2fa800f1f2ebc3353294931dc3582',\n",
       "   'face_rectangle': {'top': 355, 'left': 1102, 'width': 236, 'height': 236},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 34},\n",
       "    'smile': {'value': 94.764, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.023,\n",
       "     'disgust': 0.006,\n",
       "     'fear': 0.007,\n",
       "     'happiness': 99.334,\n",
       "     'neutral': 0.582,\n",
       "     'sadness': 0.006,\n",
       "     'surprise': 0.042}}},\n",
       "  {'face_token': 'f0148088a9e1859724499492aae6963d',\n",
       "   'face_rectangle': {'top': 222, 'left': 274, 'width': 225, 'height': 225},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 7.332, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.005,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.037,\n",
       "     'neutral': 99.958,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'ZTtQORZzaHTUrpqCHAlhMw==',\n",
       " 'face_num': 2}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results=r.json()                                                       #令result=r.json\n",
    "results                                                                #将转化后的结果输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c1e2fa800f1f2ebc3353294931dc3582</td>\n",
       "      <td>355</td>\n",
       "      <td>1102</td>\n",
       "      <td>236</td>\n",
       "      <td>236</td>\n",
       "      <td>Female</td>\n",
       "      <td>34</td>\n",
       "      <td>94.764</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.023</td>\n",
       "      <td>0.006</td>\n",
       "      <td>0.007</td>\n",
       "      <td>99.334</td>\n",
       "      <td>0.582</td>\n",
       "      <td>0.006</td>\n",
       "      <td>0.042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>f0148088a9e1859724499492aae6963d</td>\n",
       "      <td>222</td>\n",
       "      <td>274</td>\n",
       "      <td>225</td>\n",
       "      <td>225</td>\n",
       "      <td>Male</td>\n",
       "      <td>20</td>\n",
       "      <td>7.332</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.005</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.037</td>\n",
       "      <td>99.958</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  c1e2fa800f1f2ebc3353294931dc3582                 355                 1102   \n",
       "1  f0148088a9e1859724499492aae6963d                 222                  274   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   236                    236                  Female   \n",
       "1                   225                    225                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    34                  94.764                        50.0   \n",
       "1                    20                   7.332                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.023                       0.006   \n",
       "1                     0.005                       0.000   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.007                        99.334   \n",
       "1                    0.000                         0.037   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                       0.582                       0.006   \n",
       "1                      99.958                       0.000   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.042  \n",
       "1                        0.000  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd                           #调用pandas,并将其命名为pd\n",
    "df = pd.json_normalize(results,record_path='faces')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'face_token': 'f0148088a9e1859724499492aae6963d',\n",
       " 'face_rectangle': {'top': 222, 'left': 274, 'width': 225, 'height': 225},\n",
       " 'attributes': {'gender': {'value': 'Male'},\n",
       "  'age': {'value': 20},\n",
       "  'smile': {'value': 7.332, 'threshold': 50.0},\n",
       "  'emotion': {'anger': 0.005,\n",
       "   'disgust': 0.0,\n",
       "   'fear': 0.0,\n",
       "   'happiness': 0.037,\n",
       "   'neutral': 99.958,\n",
       "   'sadness': 0.0,\n",
       "   'surprise': 0.0}}}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " {'face_token': 'f0148088a9e1859724499492aae6963d',\n",
    "   'face_rectangle': {'top': 222, 'left': 274, 'width': 225, 'height': 225},\n",
    "   'attributes': {'gender': {'value': 'Male'},\n",
    "    'age': {'value': 20},\n",
    "    'smile': {'value': 7.332, 'threshold': 50.0},\n",
    "    'emotion': {'anger': 0.005,\n",
    "     'disgust': 0.0,\n",
    "     'fear': 0.0,\n",
    "     'happiness': 0.037,\n",
    "     'neutral': 99.958,\n",
    "     'sadness': 0.0,\n",
    "     'surprise': 0.0}}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '40a1d3abf72dec548fd0117be3980f0d',\n",
       " 'time_used': 163,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603520175,be69d6dd-953a-4bd6-b9dd-93452f62bac9',\n",
       " 'outer_id': 'kookv',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸集合\n",
    "import requests,json\n",
    "api_secret = \"72ZSxF3jN4mKki4f6kcTCDIjlEvcwi4S\"\n",
    "api_key = 'n5iuIaeoc-HEqvbWSEMxc73zfy092FdR'  \n",
    "display_name = \"人脸集合2020\"\n",
    "outer_id = \"kookv\"\n",
    "user_data = \"相册集\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}\n",
    "r = requests.post(CreateFace_Url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'40a1d3abf72dec548fd0117be3980f0d',\n",
    "    'face_tokens':'f0148088a9e1859724499492aae6963d',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '40a1d3abf72dec548fd0117be3980f0d',\n",
       " 'time_used': 558,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603520250,fe85a4cd-6ac7-4987-b8c5-a2ecb7979e14',\n",
       " 'outer_id': 'kookv',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '40a1d3abf72dec548fd0117be3980f0d',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 171,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603520600,8f9f57eb-8588-4064-8e8c-a0a58f807d00',\n",
       " 'outer_id': 'kookv',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.FaceSet RemoveFace（移除人脸信息）\n",
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "   'faceset_token':'40a1d3abf72dec548fd0117be3980f0d',\n",
    "    'face_tokens':'f0148088a9e1859724499492aae6963d',\n",
    "}\n",
    "r = requests.post(RemoveFace_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '40a1d3abf72dec548fd0117be3980f0d',\n",
       " 'request_id': '1603520644,67f69a30-4e48-493c-a573-f1be1a981c83',\n",
       " 'time_used': 80,\n",
       " 'outer_id': 'kookv'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.FaceSet Update（更新人脸集合信息）\n",
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "     'faceset_token':'40a1d3abf72dec548fd0117be3980f0d',\n",
    "    'user_data':\"user_data\",\n",
    "}\n",
    "r = requests.post(Update_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "jintaiheng = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603530792718&di=570db09e90c2680ca391c692b305ab32&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2Fe5cea2e8cc729a2118beae3d87f60968c6a97253.jpg\"\n",
    "tianjiuguo = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603530840282&di=8c053674d519e2303c941a0dc33e7dd0&imgtype=0&src=http%3A%2F%2Fpic.rmb.bdstatic.com%2F55684128c58cb8d84d5dde50b73fb00f.jpeg\"\n",
    "minyunqi = \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603530896451&di=1562410191c4941943d1bc0f8b7f630c&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201901%2F13%2F20190113191325_ghwwt.thumb.400_0.jpg\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 324,\n",
       "    'top': 252,\n",
       "    'left': 702,\n",
       "    'height': 324},\n",
       "   'face_token': '4bcbd2bd21b66df77902fba3012aa9dc'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 332,\n",
       "    'top': 171,\n",
       "    'left': 414,\n",
       "    'height': 332},\n",
       "   'face_token': '9fe282d43d9738ed6de856119cf3cb34'}],\n",
       " 'time_used': 1047,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 67.486,\n",
       " 'image_id2': 'KVCDpIq+mvse++gUZ7zTvg==',\n",
       " 'image_id1': 'ugZUQW9So1Da7z/Df5m/9A==',\n",
       " 'request_id': '1603520894,2052aff0-f472-4d14-8695-01aca13c0aac'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法1\n",
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':jintaiheng,\n",
    "    'image_url2':tianjiuguo\n",
    "}\n",
    "r = requests.post(Compare_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603520948,e630dc7a-f961-4f4c-a518-82b9b2a28500',\n",
       " 'time_used': 220,\n",
       " 'faces': [{'face_token': '8d382e8aef5041c792f7e837e4257185',\n",
       "   'face_rectangle': {'top': 252, 'left': 702, 'width': 324, 'height': 324},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 55.672, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 2.216,\n",
       "     'disgust': 2.066,\n",
       "     'fear': 15.077,\n",
       "     'happiness': 5.811,\n",
       "     'neutral': 2.401,\n",
       "     'sadness': 0.806,\n",
       "     'surprise': 71.624}}}],\n",
       " 'image_id': 'ugZUQW9So1Da7z/Df5m/9A==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方案2:与人脸集合进行对比\n",
    "Detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = jintaiheng\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "r = requests.post(Detect_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
